//
//  NearUserCell.m
//  HotWord
//
//  Created by Rainbow on 8/19/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "FilterTeacherCell.h"
#import "UserManager.h"

@implementation FilterTeacherCell
@synthesize backgroundView;
@synthesize headImageView;
@synthesize nickNameLabel;
@synthesize userInfoLabel;
@synthesize examLabel;
@synthesize statusLabel;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UISwipeGestureRecognizer *leftSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipecontainerView:)];
    leftSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionLeft;
    [_containerView addGestureRecognizer:leftSwipeGestureRecognizer];
    [leftSwipeGestureRecognizer release];
    
    UISwipeGestureRecognizer *rightSwipeGestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipecontainerView:)];
    rightSwipeGestureRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
    [_containerView addGestureRecognizer:rightSwipeGestureRecognizer];
    [rightSwipeGestureRecognizer release];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)swipecontainerView:(UISwipeGestureRecognizer *)swipeGestureRecognizer
{
    [UIView animateWithDuration:0.4f animations:^{
        if (swipeGestureRecognizer.direction == UISwipeGestureRecognizerDirectionLeft)
        {
            _isShowDetail = YES;
            _containerView.left = -(_containerView.width - 320 + 5);
            
        }
        else if (swipeGestureRecognizer.direction == UISwipeGestureRecognizerDirectionRight)
        {
            _isShowDetail = NO;
            _containerView.left = 0;
        }
    }];
}

- (IBAction)tapOnDetailBtn:(id)sender
{
    
    [UIView animateWithDuration:0.4f animations:^{
        if (_isShowDetail)
        {
            _containerView.left = 0;
            _detailBtn.left = 285;
        }
        else
        {
            _containerView.left = -(_containerView.width - 320 + 5);
            _detailBtn.left = _containerView.width - 320 + 5;
        }
        _isShowDetail = !_isShowDetail;
    }];
}


- (NSString *)getDistanceByLat:(NSString *)lat andLon:(NSString *)lon
{
    User *currentUser = [UserManager sharedManager].currentUser;
    if (!lat || !lon || !currentUser.latitude || !currentUser.longitude) {
        return @"unknow";
    }
    CLLocation *userLocation = [[[CLLocation alloc] initWithLatitude:[lat doubleValue]
                                                           longitude:[lon doubleValue]] autorelease];
    CLLocation *currentLocation = [[[CLLocation alloc] initWithLatitude:[currentUser.latitude doubleValue]
                                                              longitude:[currentUser.longitude doubleValue]] autorelease];
    CLLocationDistance distance = [currentLocation distanceFromLocation:userLocation];
    
    if (distance == 0) {
        return nil;
    }
    else {
        if (distance > 1000) {
            
            return [NSString stringWithFormat:@"%dkm",(NSUInteger)round(distance/1000.0)];
        }
        else {
            return [NSString stringWithFormat:@"%dm",(NSUInteger)round(distance)];
        }
        
    }
}

- (void)setUserForCell:(User *)user
{
    _containerView.left = 0;
    _detailBtn.left = 285;

    if (_user) {
        [_user release];
        _user = nil;
    }
    _user = [user retain];
    
    if (IS_STRING_NOT_EMPTY(_user.nickname))
    {
        self.nickNameLabel.text = _user.nickname;
    }
    else
    {
        self.nickNameLabel.text = @"昵称未知";
    }
    
    _companyLabel.text = _user.city;
    if (_user.status)
    {
        self.statusLabel.text = _user.status;
        CGSize contentSize = [self.statusLabel.text sizeWithFont:self.statusLabel.font
                                    constrainedToSize:CGSizeMake(self.statusLabel.width, 50)
                                        lineBreakMode:UILineBreakModeWordWrap];
        self.statusLabel.height = contentSize.height;
    }
    else
    {
        self.statusLabel.text = @"这是个懒家伙！";
        self.statusLabel.height = 19;
    }
    
    if (IS_STRING_NOT_EMPTY(_user.exam))
    {
        if (IS_STRING_NOT_EMPTY(_user.testSection))
        {
            self.examLabel.text = [NSString stringWithFormat:@"%@ | %@",_user.exam, _user.testSection,nil];
        }
        else
        {
            self.examLabel.text = _user.exam;
        }
    }
    else
    {
        self.examLabel.text = @"";
    }


    NSString *age = @"";
    if (_user.year) {
        
        age = [NSString stringWithFormat:@"%@岁", _user.year];
    }
    else {
        age = @"年龄未知";
    }
    
    NSString *gender = @"男";
    if ([@"Girl" isEqualToString:user.gender]) {
        gender = @"女";
    }

    
    NSString *distanceStr = @"";
    CGFloat distance = [_user.distance floatValue];
    if (distance >= 1.0) {
        
        distanceStr = [NSString stringWithFormat:@"%0.1fkm", distance];
    }
    else if (distance < 0)
    {
        distanceStr = @"未知";
    }
    else
    {
        self.userInfoLabel.hidden = NO;
        distance = distance *1000;
        distanceStr = [NSString stringWithFormat:@"%0.0fm", distance];
    }
    
    
    self.userInfoLabel.text = [NSString stringWithFormat:@"%@ | %@ | 师 | %@",distanceStr,gender,age];

    self.headImageView.showLoadingWithDefaultImage = YES;
    [self.headImageView setIndicatorViewStyle:UIActivityIndicatorViewStyleGray];
    [self.headImageView setDefaultImage:[UIImage imageNamed:@"photo_card.png"]];
    [self.headImageView loadImage: _user.avater];
}

- (void)dealloc {
    [_user release];
    [backgroundView release];
    [headImageView release];
    [nickNameLabel release];
    [userInfoLabel release];
    [examLabel release];
    [statusLabel release];
    [_containerView release];
    [_companyLabel release];
    [_detailBtn release];
    [super dealloc];
}
@end
